package View.JTree;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JTree;
import javax.swing.SwingWorker;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultTreeModel;

import Management.AnimationRoute;

/**
 * 展示文件路径的树，不包含文件
 * @author zmm
 *
 */
public class JAnimationTree extends JTree {
    private JAnimationTreeNode rootNode;
    private DefaultTreeModel jAnimationTreeModel;
    
    /**
     * 构造器
     * @param path
     * @param isList
     */
	public JAnimationTree(String path ) {
		this(new File(path));
	}

	/**
	 * 获得DefaultTreeModel
	 * @return DefaultTreeModel
	 */
	public DefaultTreeModel getJAnimationTreeModel() {
		return jAnimationTreeModel;
	}
	
	/**
	 * 获得rootNode
	 * @return JAnimationTreeNode
	 */
	public JAnimationTreeNode getRootNode() {
		return rootNode;
	}
	
	/**
	 * 构造器
	 * @param file
	 * @param isList
	 */
	public JAnimationTree(File file) {
		putClientProperty("JTree.lineStyle", "None");

		setCellRenderer(new MyTreeCellRenderer());
    	setOpaque(false);
        rootNode = new JAnimationTreeNode(new AnimationRoute(file.getPath()));
        rootNode.expand();
        jAnimationTreeModel = new DefaultTreeModel(rootNode);
        setModel(jAnimationTreeModel);
        addTreeExpansionListener(new JTreeExpansionListener());
	}

	/**
	 * 设置RootNode
	 * @param rootNode
	 */
	public void setRootNode(JAnimationTreeNode rootNode) {
		this.rootNode = rootNode;
	}

	/**
	 * 扩展节点监听器
	 * @author zmm
	 *
	 */
    protected class JTreeExpansionListener implements TreeExpansionListener {
    	@Override
        public void treeExpanded(TreeExpansionEvent e) {
            JAnimationTreeNode fileNode = (JAnimationTreeNode) e.getPath().getLastPathComponent();
//            System.out.println(fileNode.getFile().getPath());
            if (fileNode != null) {
                new FileNodeExpansion(fileNode).execute();
            }
        }
        
        public void treeCollapsed(TreeExpansionEvent e) {}
    }

    /**
     * 展开线程
     * @author zmm
     *
     */
    protected class FileNodeExpansion extends SwingWorker<Boolean, Void> {
        private JAnimationTreeNode node;
        public FileNodeExpansion(JAnimationTreeNode node) {
            this.node = node;
        }

        @Override
        protected  Boolean doInBackground() throws Exception {
            return node.expand();
        }

        @Override
        protected void done() {
            try {
                //节点可以展开
                if (get()) {
                    jAnimationTreeModel.reload(node);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(JFileTree.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(JFileTree.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    /**
     * test
     * @param args
     */
//    public static void main(String[] args) {
//    	JFrame jf = new JFrame("JAnimationTree");
//    	jf.add(new JAnimationTree("E:/comic"));
//    	jf.pack();
//    	jf.setVisible(true);
//    	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//    }
}
